package com.seal.yoyoshop.dao.impl;

import com.seal.yoyoshop.bin.Order;
import com.seal.yoyoshop.dao.OrderDao;
import com.seal.yoyoshop.util.DBUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class OrderDaoImpl implements OrderDao {

    @Override
    public boolean InsertOrder(Order order) {
        Date now = order.getSystime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowstr = sdf.format(now);

        String sql = "INSERT INTO `order` (total, amount, status, paytype, systime, user_id) VALUES(?, ?, ?, ?, ?, ?)";
        int row = DBUtils.update(sql, order.getTotal(), order.getAmount(), 1, 0, nowstr, order.getUserId());
        boolean isSuccess;
        if (row == 1) isSuccess = true;
        else isSuccess = false;
        return isSuccess;
    }

    @Override
    public int SelectidByOrder(Order order) {
        Date now = order.getSystime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowstr = sdf.format(now);

        String sql = "SELECT * FROM `order` WHERE total = ? AND amount = ? AND status = ? AND paytype = ? AND systime = ? AND user_id = ?";
        Order ordert = DBUtils.queryOne(sql, Order.class, order.getTotal(), order.getAmount(), order.getStatus(), order.getPaytype(), nowstr,order.getUserId());
        return ordert.getId();
    }

    @Override
    public Order SelectOrderById(int orderId) {
        String sql = "SELECT * FROM `order` WHERE id = ?";
        Order order = DBUtils.queryOne(sql,Order.class,orderId);
        return order;
    }

    @Override
    public boolean updateOrder(Order order) {
        String sql = "UPDATE `order` SET total = ?, amount = ?, status = ?,paytype = ?, name = ?, phone = ?, address = ?,user_id = ? WHERE id = ?";
        int row = DBUtils.update(sql, order.getTotal(), order.getAmount(), order.getStatus(), order.getPaytype(), order.getName(), order.getPhone(), order.getAddress(), order.getUserId(), order.getId());
        if(row == 1)return true;
        return false;
    }

    @Override
    public List<Order> SelectOrderByUserId(Integer userid) {
        String sql = "SELECT * FROM `order` WHERE user_id = ? AND status != 0";
        List<Order> orderList = DBUtils.query(sql,Order.class,userid);
        return orderList;
    }

    @Override
    public List<Order> SelectOrderList(Integer page, Integer size) {
        String sql = "SELECT * FROM `order` WHERE status != 0 LIMIT ?, ?";
        List<Order> orderList = DBUtils.query(sql,Order.class, (page-1) * size, size);
        return orderList;
    }

    @Override
    public long selectCount() {
        String sql = "SELECT count(*) AS count FROM `order` WHERE status != 0";
        Map<String, Object> map = DBUtils.queryOne(sql);
        long count = (long)map.get("count");
        return count;
    }

    @Override
    public List<Order> SelectOrderList(Integer status, Integer page, Integer size) {
        String sql = "SELECT * FROM `order` WHERE status = ? LIMIT ?, ?";
        List<Order> orderList = DBUtils.query(sql,Order.class, status,  (page-1) * size, size);
        return orderList;
    }

    @Override
    public long selectCount(Integer status) {
        String sql = "SELECT count(*) AS count FROM `order` WHERE status = ?";
        Map<String, Object> map = DBUtils.queryOne(sql,status);
        long count = (long)map.get("count");
        return count;
    }

}
